//
//  ShellMineHeadView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/5/15.
//  Copyright © 2019 吕VV. All rights reserved.
//

#import "ShellMineHeadView.h"
#import "ShellMineViewModel.h"
#import "ShellSettingViewController.h"
#import "ShellSysMessageViewController.h"  //站内信
#import "ShellNodeViewController.h"         //等级

@interface  ShellMineHeadView()

@property (nonatomic,strong) ShellMineViewModel *viewModel;

//頭像
@property (nonatomic,strong) UIButton *headBtn;

//名字
@property (nonatomic,strong) UILabel *nameLabel;

//等级
@property (nonatomic,strong) UILabel *levelLabel;


//number
@property (nonatomic,strong) UILabel *numberLabel;
@end

@implementation ShellMineHeadView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellMineViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{

    [self addSubview:self.headBtn];
    [self.headBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(55);
        make.width.height.mas_equalTo(70);
        make.left.mas_equalTo(25);
    }];
    
    
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(65);
        make.width.mas_equalTo(170);
        make.height.mas_equalTo(25);
        make.left.mas_equalTo(self.headBtn.mas_right).mas_offset(15);
    }];
    
    
    ///!!!:等级
    UIButton *levelBtn = [UIButton lz_buttonImageName:@"" backgroundImageName:@""];
    levelBtn.tag = 201900;
    [levelBtn setGradientBackgroundWithColors:@[UIColorHex(0xFFE482),UIColorHex(0xFF9559)] locations:@[@(0),@(1.0)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [self addSubview:levelBtn];
    [levelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_offset(7);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(self.headBtn.mas_right).mas_offset(15);
    }];
    levelBtn.cornerRadius = 10;

    
    UIImageView *levelImgView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_zuanshi")];
    [levelBtn addSubview:levelImgView];
    [levelImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(levelBtn.mas_centerY);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(15);
        make.left.mas_equalTo(6);
    }];

    [levelBtn addSubview:self.levelLabel];
    [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(levelBtn.mas_centerY);
        make.width.mas_equalTo(45);
        make.height.mas_equalTo(17);
        make.left.mas_equalTo(levelImgView.mas_right).mas_offset(2);
    }];
    
    //通知
    UIButton *nataionBtn = [UIButton lz_buttonImageName:@"" backgroundImageName:@"icon_nataion"];
    [self addSubview:nataionBtn];
    [nataionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
        make.right.mas_equalTo(-66);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    [self addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nataionBtn.mas_top).mas_offset(0);
        make.right.mas_equalTo(nataionBtn.mas_right).mas_offset(6);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    
    
    //设置
    UIButton *setBtn = [UIButton lz_buttonImageName:@"" backgroundImageName:@"icon_setting"];
    [self addSubview:setBtn];
    [setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
        make.right.mas_equalTo(-22);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    
    
    
    //刷新头像. 昵称, 邮箱, 通知
    //更新数据//昵称
    @weakify(self);
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPUserInfoWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        [self refreshUI];
    }];
    
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self refreshUI];
    }];

    [self refreshUI];
    
    [[nataionBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
       
        ShellSysMessageViewController *sysMessageVC = [[ShellSysMessageViewController alloc] init];
        [LZTool.currentViewController.navigationController pushViewController:sysMessageVC animated:YES];
    }];
    
    [[setBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        ShellSettingViewController *setVC = [[ShellSettingViewController alloc] init];
        [LZTool.currentViewController.navigationController pushViewController:setVC animated:YES];
        
    }];
    
    [[levelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        ShellNodeViewController *nodeVC = [[ShellNodeViewController alloc] init];
        nodeVC.levelDictionary = self.viewModel.levelDictionary;
        [LZTool.currentViewController.navigationController pushViewController:nodeVC animated:YES];
         
     }];
    
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);

        if(self.viewModel.unReadNumber > 0){
            self.numberLabel.hidden = NO;
            self.numberLabel.text = [NSString stringWithFormat:@"%ld",(long)self.viewModel.unReadNumber];
        } else {
            self.numberLabel.hidden = YES;
        }
    }];
    
   
}



#pragma mark - actions
- (void)refreshUI {
    
    ShellUserDetailModel *infoModel = [LZToolCache getUserInfo];
    self.nameLabel.text = infoModel.name;
    UIButton *levelBtn = [self viewWithTag:201900];
    if ([infoModel.showLevel isEqualToString:@"Y"]) {
        levelBtn.hidden = NO;
    } else {
        levelBtn.hidden = YES;
    }
    self.levelLabel.text = self.viewModel.levelDictionary[@"levelString"];
    [self.headBtn sd_setImageWithURL:[NSURL URLWithString:infoModel.portrait] forState:UIControlStateNormal placeholderImage:k_imageName(@"default_headportrait")];
    self.numberLabel.text = [NSString stringWithFormat:@"%ld",(long)self.viewModel.unReadNumber];
}
#pragma mark - custool

#pragma mark - get && set

- (UIButton *)headBtn{
    if(!_headBtn){
        _headBtn = [UIButton lz_buttonImageName:@"default_headportrait" backgroundImageName:@""];
        _headBtn.cornerRadius = 35;
        _headBtn.adjustsImageWhenHighlighted = NO;
    }
    return _headBtn;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textBoldFont(18)];
    }
    return _nameLabel;
}

- (UILabel *)levelLabel{
    if (!_levelLabel) {
        _levelLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(12)];
    }
    return _levelLabel;
}

- (UILabel *)numberLabel{
    if (!_numberLabel) {
        _numberLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(11)];
        [_numberLabel lz_centerAlignment];
        _numberLabel.backgroundColor = UIColorHex(0xF4376D);
        _numberLabel.cornerRadius = 8;
    }
    return _numberLabel;
}


@end
